package com.se7en.core.utils;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class AuthInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object handler) throws Exception {
        String uri = req.getRequestURI();
        List<String> passUrls = new ArrayList<String>();
        passUrls.add(".css");
        passUrls.add(".js");
        passUrls.add(".gif");
        passUrls.add(".jpg");
        passUrls.add(".png");
        passUrls.add("login");
        passUrls.add(".pdf");
        passUrls.add(".doc");
        passUrls.add(".exe");
        passUrls.add(".html");
        boolean needAuth = true;
        for (String url : passUrls) {
            if (uri.indexOf(url) != -1) {
                needAuth = false;
                break;
            }
        }

        if (needAuth) {

            System.out.println(DateUtils.currentDatetime() + "  Pre  URL：" + uri);

        }
        return super.preHandle(req, res, handler);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        // System.out.println(DateUtils.currentDatetime()+"  Post  URL：" +
        // request.getRequestURI());
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {/*
                               * String uri = request.getRequestURI();
                               * List<String> passUrls = new
                               * ArrayList<String>(); passUrls.add(".css");
                               * passUrls.add(".js"); passUrls.add(".gif");
                               * passUrls.add(".jpg"); passUrls.add(".png");
                               * passUrls.add("login"); passUrls.add("toLogin");
                               * passUrls.add("register");
                               * passUrls.add("check");
                               * passUrls.add("saveRegister");
                               * passUrls.add(".pdf"); passUrls.add(".doc");
                               * passUrls.add(".exe"); passUrls.add(".html");
                               * passUrls.add("/publish/placard/jscdlist");
                               * boolean needAuth = true; for (String url :
                               * passUrls) { if (uri.indexOf(url) != -1) {
                               * needAuth = false; break; } } if (needAuth) {
                               * System.out.println(DateUtils.currentDatetime()
                               * + "  After  URL：" + request.getRequestURI()); }
                               */
    }

}
